//
//  ViewController.m
//  gra
//
//  Created by xingshuai on 2021/3/30.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) CAGradientLayer *gradientLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // label
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30)];
    titleLabel.text = @"上海市北京省";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:20];
    [self.view addSubview:titleLabel];
    CGAffineTransform matrix = CGAffineTransformMake(1, 0, tanf(10 * (CGFloat)M_PI / 180), 1, 0, 0);
    UIFontDescriptor *desc = [UIFontDescriptor fontDescriptorWithName:[UIFont fontWithName:@"PingFangSC-Medium" size:20].fontName matrix:matrix];
    titleLabel.font = [UIFont fontWithDescriptor:desc size:20];
    
    [self TextGradientview:titleLabel bgVIew:self.view gradientColors:@[(id)[UIColor purpleColor].CGColor, (id)[UIColor yellowColor].CGColor] gradientStartPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
    
    // button
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 100)];
    [self.view addSubview:btn];
    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:30];
    [btn setTitle:@"button文字渐变色" forState:UIControlStateNormal];
    [self TextGradientControl:btn bgVIew:self.view gradientColors:@[(id)[UIColor purpleColor].CGColor, (id)[UIColor orangeColor].CGColor, (id)[UIColor yellowColor].CGColor] gradientStartPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
}

/*
 view 是要设置渐变字体的控件   bgVIew是view的父视图  colors是渐变的组成颜色  startPoint是渐变开始点 endPoint结束点
 */
-(void)TextGradientview:(UIView *)view bgVIew:(UIView *)bgVIew gradientColors:(NSArray *)colors gradientStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint{
    
    CAGradientLayer* gradientLayer1 = [CAGradientLayer layer];
    gradientLayer1.frame = view.frame;
    gradientLayer1.colors = colors;
    gradientLayer1.locations = @[@(0), @(1)];
    gradientLayer1.startPoint =startPoint;
    gradientLayer1.endPoint = endPoint;
    [bgVIew.layer addSublayer:gradientLayer1];
    gradientLayer1.mask = view.layer;
    view.frame = gradientLayer1.bounds;
}

/*
 control 是要设置渐变字体的控件   bgVIew是view的父视图  colors是渐变的组成颜色  startPoint是渐变开始点 endPoint结束点
 */
-(void)TextGradientControl:(UIControl *)control bgVIew:(UIView *)bgVIew gradientColors:(NSArray *)colors gradientStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint{

    CAGradientLayer* gradientLayer1 = [CAGradientLayer layer];
    gradientLayer1.frame = control.frame;
    gradientLayer1.colors = colors;
    gradientLayer1.locations = @[@(0), @(0.5), @(1)];
    gradientLayer1.startPoint =startPoint;
    gradientLayer1.endPoint = endPoint;
    [bgVIew.layer addSublayer:gradientLayer1];
    gradientLayer1.mask = control.layer;
    control.frame = gradientLayer1.bounds;
}



@end
